package com.chenfan.ccp.plug.mybatis.util;

import com.chenfan.ccp.plug.mybatis.annotation.Column;
import com.chenfan.ccp.plug.mybatis.annotation.Table;
import com.chenfan.ccp.plug.mybatis.annotation.Transient;
import com.chenfan.ccp.plug.mybatis.config.MybatisConfig;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * mybatisPlus工具
 */
public class MybatisPlusUtil {
	private static final char UNDERLINE = '_';
	private static final Map<String, List<Field>> ALL_CLASS_CACHE_FIELD = new ConcurrentHashMap<>();

	private MybatisPlusUtil() {

	}

	/**
	 * 字段转列名
	 *
	 * @param field 字段的名称
	 * @return
	 */
	public static String field2ColumnName(Field field) {
		String columnName = "";

		if (field.getAnnotation(Transient.class) != null) {
			return columnName;
		}

		Column annotation = field.getAnnotation(Column.class);
		if (annotation != null && !annotation.value().isEmpty()) {
			columnName = annotation.value();
		} else {
			columnName = field.getName();
			if (MybatisConfig.getIsCamel()) {
				columnName = camel2Underline(columnName);
			}
		}
		return columnName;

	}

	/**
	 * 驼峰格式字符串转换为下划线格式字符串
	 *
	 * @param param
	 * @return
	 */
	protected static String camel2Underline(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}


	/**
	 * 根据类获取表名
	 *
	 * @param superClass
	 * @return
	 */
	public static String getTableNameByClass(Class<?> superClass) {
		String tableName;
		Table annotation = superClass.getAnnotation(Table.class);
		String prefix = (annotation == null || annotation.prefix().trim().isEmpty()) ? "" : annotation.prefix().trim() + "_";
		if (annotation != null && !annotation.value().isEmpty()) {
			tableName = annotation.value().trim();
		} else {
			tableName = lowerFirstCase(superClass.getSimpleName());
			if (MybatisConfig.getIsCamel()) {
				tableName = camel2Underline(tableName);
			}

		}
		return prefix + tableName;
	}

	/**
	 * 根据类获取所有的私有属性
	 *
	 * @param aClass 需要获取的类
	 * @return
	 */
	public static List<Field> getAllFields(Class<?> aClass) {

		String classCompleteName = aClass.getCanonicalName();
		List<Field> fieldList = ALL_CLASS_CACHE_FIELD.getOrDefault(classCompleteName, new ArrayList<>());

		if (!fieldList.isEmpty()) {
			return fieldList;
		}

		Field[] fieldArr = aClass.getDeclaredFields();

		for (Field field : fieldArr) {
			if (Modifier.isFinal(field.getModifiers())) {
				continue;
			}

			fieldList.add(field);

		}

		while (!aClass.getCanonicalName().equals(Object.class.getCanonicalName())) {
			aClass = aClass.getSuperclass();
			fieldArr = aClass.getDeclaredFields();
			for (Field field : fieldArr) {
				if (Modifier.isProtected(field.getModifiers()) || Modifier.isProtected(field.getModifiers())
					&& !Modifier.isFinal(field.getModifiers())) {
					fieldList.add(field);
				}


			}


		}

		ALL_CLASS_CACHE_FIELD.put(classCompleteName, fieldList);

		return fieldList;
	}


	/**
	 * 首字母小写
	 *
	 * @param name 需要首字母小写的名称
	 * @return
	 */
	protected static String lowerFirstCase(String name) {
		char[] chars = name.toCharArray();
		chars[0] += 32;
		return String.valueOf(chars);
	}
}
